home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / indent.c < prev    next >
C/C++ Source or Header  |  1992-07-05  |  17KB  |  687 lines

  1. /* Indentation functions.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #include "buffer.h"
  24. #include "indent.h"
  25. #include "window.h"
  26. #include "termchar.h"
  27. #include "termopts.h"
  28.  
  29. #define CR '\015'
  30.  
  31. /* Indentation can insert tabs if this is non-zero;
  32.    otherwise always uses spaces */
  33. int indent_tabs_mode;
  34.  
  35. #define min(a, b) ((a) < (b) ? (a) : (b))
  36. #define max(a, b) ((a) > (b) ? (a) : (b))
  37.  
  38. #ifdef EIGHT_BIT
  39. /* Visible characters */
  40. extern char visible[];
  41. #endif
  42.  
  43. /* These three values memoize the current column to avoid recalculation */
  44. /* Some things in set last_known_column_point to -1
  45.   to mark the memoized value as invalid */
  46. /* Last value returned by current_column */
  47. int last_known_column;
  48. /* Value of point when current_column was called */
  49. int last_known_column_point;
  50. /* Value of MODIFF when current_column was called */
  51. int last_known_column_modified;
  52.  
  53. extern int minibuf_prompt_width;
  54.  
  55. DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
  56.   "Return the horizontal position of point.  Beginning of line is column 0.\n\
  57. This is calculated by adding together the widths of all the displayed\n\
  58. representations of the character between the start of the previous line\n\
  59. and point.  (eg control characters will have a width of 2 or 4, tabs\n\
  60. will have a variable width)\n\
  61. Ignores finite width of screen, which means that this function may return\n\
  62. values greater than (screen-width).\n\
  63. Whether the line is visible (if `selective-display' is t) has no effect.")
  64.   ()
  65. {
  66.   Lisp_Object temp;
  67.   XFASTINT (temp) = current_column ();
  68.   return temp;
  69. }
  70.  
  71. /* Cancel any recorded value of the horizontal position.  */
  72.  
  73. invalidate_current_column ()
  74. {
  75.   last_known_column_point = 0;
  76. }
  77.  
  78. int
  79. current_column ()
  80. {
  81.   register int col;
  82.   register unsigned char *ptr, *stop, c;
  83.   register int tab_seen;
  84.   register int post_tab;
  85.   register int tab_width = XINT (current_buffer->tab_width);
  86.   int ctl_arrow = !NULL (current_buffer->ctl_arrow);
  87.  
  88.   if (point == last_known_column_point
  89.       && MODIFF == last_known_column_modified)
  90.     return last_known_column;
  91.  
  92.   /* Make a pointer for decrementing through the chars before point.  */
  93.   ptr = &FETCH_CHAR (point - 1) + 1;
  94.   /* Make a pointer to where consecutive chars leave off,
  95.      going backwards from point.  */
  96.   if (point == BEGV)
  97.     stop = ptr;
  98.   else if (point <= GPT || BEGV > GPT)
  99.     stop = BEGV_ADDR;
  100.   else
  101.     stop = GAP_END_ADDR;
  102.  
  103.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  104.  
  105.   col = 0, tab_seen = 0, post_tab = 0;
  106.  
  107.   while (1)
  108.     {
  109.       if (ptr == stop)
  110.     {
  111.       /* We stopped either for the beginning of the buffer
  112.          or for the gap.  */
  113.       if (ptr == BEGV_ADDR)
  114.         break;
  115.       /* It was the gap.  Jump back over it.  */
  116.       stop = BEGV_ADDR;
  117.       ptr = GPT_ADDR;
  118.       /* Check whether that brings us to beginning of buffer.  */
  119.       if (BEGV >= GPT) break;
  120.     }
  121.  
  122.       c = *--ptr;
  123. #ifdef EIGHT_BIT
  124.       if (visible[c])
  125. #else
  126.       if (c >= 040 && c < 0177)
  127. #endif
  128.     {
  129.       col++;
  130.     }
  131.       else if (c == '\n')
  132.     break;
  133.       else if (c == '\r' && EQ (current_buffer->selective_display, Qt))
  134.     break;
  135.       else if (c == '\t')
  136.     {
  137.       if (tab_seen)
  138.         col = ((col + tab_width) / tab_width) * tab_width;
  139.  
  140.       post_tab += col;
  141.       col = 0;
  142.       tab_seen = 1;
  143.     }
  144.       else
  145.     col += (ctl_arrow && c < 0200) ? 2 : 4;
  146.     }
  147.  
  148.   if (tab_seen)
  149.     {
  150.       col = ((col + tab_width) / tab_width) * tab_width;
  151.       col += post_tab;
  152.     }
  153.  
  154.   last_known_column = col;
  155.   last_known_column_point = point;
  156.   last_known_column_modified = MODIFF;
  157.  
  158.   return col;
  159. }
  160.  
  161. ToCol (col)
  162.      int col;
  163. {
  164.   register int fromcol = current_column ();
  165.   register int n;
  166.   register int tab_width = XINT (current_buffer->tab_width);
  167.  
  168.   if (fromcol > col)
  169.     return;
  170.  
  171.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  172.  
  173.   if (indent_tabs_mode)
  174.     {
  175.       n = col / tab_width - fromcol / tab_width;
  176.       if (n)
  177.     {
  178.       while (n-- > 0)
  179.         insert ("\t", 1);
  180.  
  181.       fromcol = (col / tab_width) * tab_width;
  182.     }
  183.     }
  184.  
  185.   while (fromcol < col)
  186.     {
  187.       insert ("        ", min (8, col - fromcol));
  188.       fromcol += min (8, col - fromcol);
  189.     }
  190.  
  191.   last_known_column = col;
  192.   last_known_column_point = point;
  193.   last_known_column_modified = MODIFF;
  194. }
  195.  
  196. DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
  197.   "Indent from point with tabs and spaces until COLUMN is reached.\n\
  198. Always do at least MIN spaces even if that goes past COLUMN;\n\
  199. by default, MIN is zero.")
  200.   (col, minimum)
  201.      Lisp_Object col, minimum;
  202. {
  203.   int mincol;
  204.   register int fromcol;
  205.   register int tab_width = XINT (current_buffer->tab_width);
  206.  
  207.   CHECK_NUMBER (col, 0);
  208.   if (NULL (minimum))
  209.     XFASTINT (minimum) = 0;
  210.   CHECK_NUMBER (minimum, 1);
  211.  
  212.   fromcol = current_column ();
  213.   mincol = fromcol + XINT (minimum);
  214.   if (mincol < XINT (col)) mincol = XINT (col);
  215.  
  216.   if (fromcol == mincol)
  217.     return make_number (fromcol);
  218.  
  219.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  220.  
  221.   if (indent_tabs_mode)
  222.     {
  223.       Lisp_Object n;
  224.       XFASTINT (n) = mincol / tab_width - fromcol / tab_width;
  225.       if (XFASTINT (n) != 0)
  226.     {
  227.       Finsert_char (make_number ('\t'), n);
  228.  
  229.       fromcol = (mincol / tab_width) * tab_width;
  230.     }
  231.     }
  232.  
  233.   XFASTINT (col) = mincol - fromcol;
  234.   Finsert_char (make_number (' '), col);
  235.  
  236.   last_known_column = mincol;
  237.   last_known_column_point = point;
  238.   last_known_column_modified = MODIFF;
  239.  
  240.   XSETINT (col, mincol);
  241.   return col;
  242. }
  243.  
  244. DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
  245.   0, 0, 0,
  246.   "Return the indentation of the current line.\n\
  247. This is the horizontal position of the character\n\
  248. following any initial whitespace.")
  249.   ()
  250. {
  251.   Lisp_Object val;
  252.  
  253.   XFASTINT (val) = position_indentation (find_next_newline (point, -1));
  254.   return val;
  255. }
  256.  
  257. position_indentation (pos)
  258.      register int pos;
  259. {
  260.   register int column = 0;
  261.   register int tab_width = XINT (current_buffer->tab_width);
  262.   register unsigned char *p;
  263.   register unsigned char *stop;
  264.  
  265.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  266.  
  267.   stop = &FETCH_CHAR (BufferSafeCeiling (pos)) + 1;
  268.   p = &FETCH_CHAR (pos);
  269.   while (1)
  270.     {
  271.       while (p == stop)
  272.     {
  273.       if (pos == ZV)
  274.         return column;
  275.       pos += p - &FETCH_CHAR (pos);
  276.       p = &FETCH_CHAR (pos);
  277.       stop = &FETCH_CHAR (BufferSafeCeiling (pos)) + 1;
  278.     }
  279.       switch (*p++)
  280.     {
  281.     case ' ':
  282.       column++;
  283.       break;
  284.     case '\t':
  285.       column += tab_width - column % tab_width;
  286.       break;
  287.     default:
  288.       return column;
  289.     }
  290.     }
  291. }
  292.  
  293. DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 1, 0,
  294.   "Move point to column COLUMN in the current line.\n\
  295. COLUMN is calculated by adding together the widths of all the displayed\n\
  296. representations of the character between the start of the previous line\n\
  297. and point.  (eg control characters will have a width of 2 or 4, tabs\n\
  298. will have a variable width)\n\
  299. Ignores finite width of screen, which means that this function may be\n\
  300. passed values greater than (screen-width)")
  301.   (column)
  302.      Lisp_Object column;
  303. {
  304.   register int pos = point;
  305.   register int col = current_column ();
  306.   register int goal;
  307.   register int end = ZV;
  308.   register int tab_width = XINT (current_buffer->tab_width);
  309.   register int ctl_arrow = !NULL (current_buffer->ctl_arrow);
  310.  
  311.   Lisp_Object val;
  312.  
  313.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  314.   CHECK_NUMBER (column, 0);
  315.   goal = XINT (column);
  316.   if (col > goal)
  317.     {
  318.       pos = find_next_newline (pos, -1);
  319.       col = 0;
  320.     }
  321.  
  322.   while (col < goal && pos < end)
  323.     {
  324.       int c = FETCH_CHAR (pos);
  325.       if (c == '\n')
  326.     break;
  327.       if (c == '\r' && EQ (current_buffer->selective_display, Qt))
  328.     break;
  329.       pos++;
  330.       col++;
  331.       if (c == '\t')
  332.     {
  333.       col += tab_width - 1;
  334.       col = col / tab_width * tab_width;
  335.     }
  336.       else if (ctl_arrow && (c < 040 || c == 0177))
  337.         col++;
  338. #ifdef EIGHT_BIT
  339.       else if (!visible[c])
  340. #else
  341.       else if (c < 040 || c >= 0177)
  342. #endif
  343.         col += 3;
  344.     }
  345.  
  346.   SET_PT (pos);
  347.  
  348.   last_known_column = col;
  349.   last_known_column_point = point;
  350.   last_known_column_modified = MODIFF;
  351.  
  352.   XFASTINT (val) = col;
  353.   return val;
  354. }
  355.  
  356. struct position val_compute_motion;
  357.  
  358. struct position *
  359. compute_motion (from, fromvpos, fromhpos, to, tovpos, tohpos, width, hscroll, tab_offset)
  360.      int from, fromvpos, fromhpos, to, tovpos, tohpos;
  361.      register int width;
  362.      int hscroll, tab_offset;
  363. {
  364.   register int hpos = fromhpos;
  365.   register int vpos = fromvpos;
  366.  
  367.   register int pos;
  368.   register int c;
  369.   register int tab_width = XFASTINT (current_buffer->tab_width);
  370.   register int ctl_arrow = !NULL (current_buffer->ctl_arrow);
  371.   int selective
  372.     = XTYPE (current_buffer->selective_display) == Lisp_Int
  373.       ? XINT (current_buffer->selective_display)
  374.     : !NULL (current_buffer->selective_display) ? -1 : 0;
  375.   int prev_vpos, prev_hpos;
  376.  
  377.   if (tab_width <= 0 || tab_width > 20) tab_width = 8;
  378.   for (pos = from; pos < to; pos++)
  379.     {
  380.       /* Stop if past the target screen position.  */
  381.       if (vpos > tovpos
  382.       || (vpos == tovpos && hpos >= tohpos))
  383.     break;
  384.  
  385.       prev_vpos = vpos;
  386.       prev_hpos = hpos;
  387.  
  388.       c = FETCH_CHAR (pos);
  389. #ifdef EIGHT_BIT
  390.       if (visible[c])
  391. #else
  392.       if (c >= 040 && c < 0177)
  393. #endif
  394.         {
  395.       unsigned char *p;
  396.       int gap_pos;
  397.       int maxhpos;
  398.  
  399.       if (vpos == tovpos)
  400.         {
  401.           maxhpos = tohpos;
  402.           if (maxhpos > width) maxhpos = width;
  403.         }
  404.       else maxhpos = width;
  405.  
  406.       if (pos < GPT)
  407.         {
  408.           gap_pos = GPT;
  409.           p = BEG_ADDR + pos;
  410.         }
  411.       else
  412.         {
  413.           gap_pos = -1;
  414.           p = GAP_SIZE + BEG_ADDR + pos;
  415.         }
  416.       do
  417.         {
  418.           hpos++;
  419.           if (hpos >= maxhpos)
  420.           {
  421.           prev_hpos = hpos - 1;
  422.           if (hpos >= width) goto check_hpos;
  423.           /* We've reached the target pos */
  424.           pos++;
  425.           goto done;
  426.           }
  427.           pos++;
  428.           if (pos >= to)
  429.             {
  430.           prev_hpos = hpos - 1;
  431.           goto done;
  432.             }
  433.           if (gap_pos == pos) p += GAP_SIZE;
  434.           c = *p++;
  435.         }
  436.       while (visible[c]);
  437.       prev_hpos = hpos;
  438.         }
  439.       if (c == '\t')
  440.     {
  441.       hpos += tab_width - ((hpos + tab_offset + hscroll - (hscroll > 0)
  442.                 /* Add tab_width here to make sure positive.
  443.                    hpos can be negative after continuation
  444.                    but can't be less than -tab_width.  */
  445.                 + tab_width)
  446.                    % tab_width);
  447.     }
  448.       else if (c == '\n')
  449.     {
  450.       if (selective > 0 && position_indentation (pos + 1) >= selective)
  451.         {
  452.           /* Skip any number of invisible lines all at once */
  453.           do
  454.         {
  455.           while (++pos < to && FETCH_CHAR (pos) != '\n');
  456.         }
  457.           while (selective > 0 && position_indentation (pos + 1) >= selective);
  458.           pos--;
  459.           /* Allow for the " ..." that is displayed for them. */
  460.           if (!NULL (current_buffer->selective_display_ellipses))
  461.         {
  462.           hpos += 4;
  463.           if (hpos >= width)
  464.             hpos = width;
  465.         }
  466.         }
  467.       else
  468.         {
  469.           /* A visible line.  */
  470.           vpos++;
  471.           hpos = 0;
  472.         }
  473.       hpos -= hscroll;
  474.       if (hscroll > 0) hpos++; /* Count the ! on column 0 */
  475.       tab_offset = 0;
  476.     }
  477.       else if (c == CR && selective < 0)
  478.     {
  479.       /* In selective display mode,
  480.          everything from a ^M to the end of the line is invisible */
  481.       while (pos < to && FETCH_CHAR (pos) != '\n') pos++;
  482.       /* Stop *before* the real newline.  */
  483.       pos--;
  484.       /* Allow for the " ..." that is displayed for them. */
  485.       if (!NULL (current_buffer->selective_display_ellipses))
  486.         {
  487.           hpos += 4;
  488.           if (hpos >= width)
  489.         hpos = width;
  490.         }
  491.     }
  492.       else
  493.     hpos += (ctl_arrow && c < 0200) ? 2 : 4;
  494.  
  495. check_hpos:
  496.       /* Handle right margin.  */
  497.       if (hpos >= width
  498.       && (hpos > width
  499.           || (pos < ZV - 1
  500.           && FETCH_CHAR (pos + 1) != '\n')))
  501.     {
  502.       if (vpos > tovpos
  503.           || (vpos == tovpos && hpos >= tohpos))
  504.         break;
  505.       if (hscroll
  506.           || (truncate_partial_width_windows
  507.           && width + 1 < screen_width)
  508.           || !NULL (current_buffer->truncate_lines))
  509.         {
  510.           /* Truncating: skip to newline.  */
  511.           while (pos < to && FETCH_CHAR (pos) != '\n') pos++;
  512.           pos--;
  513.         }
  514.       else
  515.         {
  516.           /* Continuing.  */
  517.           vpos++;
  518.           hpos -= width;
  519.           tab_offset += width;
  520.         }
  521.  
  522.     }
  523.     }
  524.   done:
  525.  
  526.   val_compute_motion.bufpos = pos;
  527.   val_compute_motion.hpos = hpos;
  528.   val_compute_motion.vpos = vpos;
  529.   val_compute_motion.prevhpos = prev_hpos;
  530.  
  531.   /* Nonzero if have just continued a line */
  532.   val_compute_motion.contin
  533.     = (pos != from
  534.        && (val_compute_motion.vpos != prev_vpos)
  535.        && c != '\n');
  536.  
  537.   return &val_compute_motion;
  538. }
  539.  
  540. pos_tab_offset (w, pos)
  541.      struct window *w;
  542.      register int pos;
  543. {
  544.   int opoint = point;
  545.   int col;
  546.  
  547.   if (pos == BEGV || FETCH_CHAR (pos - 1) == '\n')
  548.     return 0;
  549.   SET_PT (pos);
  550.   col = current_column ();
  551.   SET_PT (opoint);
  552.   return col - (col % (XFASTINT (w->width) - 1));
  553. }
  554.  
  555. /* start_hpos is the hpos of the first character of the buffer:
  556.    zero except for the minibuffer window,
  557.    where it is the width of the prompt.  */
  558.  
  559. struct position val_vmotion;
  560.  
  561. struct position *
  562. vmotion (from, vtarget, width, hscroll, window)
  563.      register int from, vtarget, width;
  564.      int hscroll;
  565.      Lisp_Object window;
  566. {
  567.   struct position pos;
  568.   /* vpos is cumulative vertical position, changed as from is changed */
  569.   register int vpos = 0;
  570.   register int prevline;
  571.   register int first;
  572.   int lmargin = hscroll > 0 ? 1 - hscroll : 0;
  573.   int selective
  574.     = XTYPE (current_buffer->selective_display) == Lisp_Int
  575.       ? XINT (current_buffer->selective_display)
  576.     : !NULL (current_buffer->selective_display) ? -1 : 0;
  577.   int start_hpos = (EQ (window, minibuf_window) ? minibuf_prompt_width : 0);
  578.  
  579.  retry:
  580.   if (vtarget > vpos)
  581.     {
  582.       /* Moving downward is simple, but must calculate from beg of line 
  583.      to determine hpos of starting point */
  584.       if (from > BEGV && FETCH_CHAR (from - 1) != '\n')
  585.     {
  586.       prevline = find_next_newline (from, -1);
  587.       while (selective > 0
  588.          && prevline > BEGV
  589.          && position_indentation (prevline) >= selective)
  590.         prevline = find_next_newline (prevline - 1, -1);
  591.       pos = *compute_motion (prevline, 0,
  592.                  lmargin + (prevline == 1 ? start_hpos : 0),
  593.                  from, 1 << (INTBITS - 2), 0,
  594.                  width, hscroll, 0);
  595.     }
  596.       else
  597.     {
  598.       pos.hpos = lmargin + (from == 1 ? start_hpos : 0);
  599.       pos.vpos = 0;
  600.     }
  601.       return compute_motion (from, vpos, pos.hpos,
  602.                  ZV, vtarget, - (1 << (INTBITS - 2)),
  603.                  width, hscroll, pos.vpos * width);
  604.     }
  605.  
  606.   /* To move upward, go a line at a time until
  607.      we have gone at least far enough */
  608.  
  609.   first = 1;
  610.  
  611.   while ((vpos > vtarget || first) && from > BEGV)
  612.     {
  613.       prevline = from;
  614.       while (1)
  615.     {
  616.       prevline = find_next_newline (prevline - 1, -1);
  617.       if (prevline == BEGV
  618.           || selective <= 0
  619.           || position_indentation (prevline) < selective)
  620.         break;
  621.     }
  622.       pos = *compute_motion (prevline, 0,
  623.                  lmargin + (prevline == 1 ? start_hpos : 0),
  624.                  from, 1 << (INTBITS - 2), 0,
  625.                  width, hscroll, 0);
  626.       vpos -= pos.vpos;
  627.       first = 0;
  628.       from = prevline;
  629.     }
  630.  
  631.   /* If we made exactly the desired vertical distance,
  632.      or if we hit beginning of buffer,
  633.      return point found */
  634.   if (vpos >= vtarget)
  635.     {
  636.       val_vmotion.bufpos = from;
  637.       val_vmotion.vpos = vpos;
  638.       val_vmotion.hpos = lmargin;
  639.       val_vmotion.contin = 0;
  640.       val_vmotion.prevhpos = 0;
  641.       return &val_vmotion;
  642.     }
  643.   
  644.   /* Otherwise find the correct spot by moving down */
  645.   goto retry;
  646. }
  647.  
  648. DEFUN ("vertical-motion", Fvertical_motion, Svertical_motion, 1, 1, 0,
  649.   "Move to start of screen line LINES lines down.\n\
  650. If LINES is negative, this is moving up.\n\
  651. Sets point to position found; this may be start of line\n\
  652.  or just the start of a continuation line.\n\
  653. Returns number of lines moved; may be closer to zero than LINES\n\
  654.  if beginning or end of buffer was reached.")
  655.   (lines)
  656.      Lisp_Object lines;
  657. {
  658.   struct position pos;
  659.   register struct window *w = XWINDOW (selected_window);
  660.  
  661.   CHECK_NUMBER (lines, 0);
  662.  
  663.   pos = *vmotion (point, XINT (lines),
  664.           XFASTINT (w->width) - 1
  665.           - (XFASTINT (w->width) + XFASTINT (w->left)
  666.              != XFASTINT (XWINDOW (minibuf_window)->width)),
  667.           /* Not XFASTINT since perhaps could be negative */
  668.           XINT (w->hscroll), selected_window);
  669.  
  670.   SET_PT (pos.bufpos);
  671.   return make_number (pos.vpos);
  672. }
  673.  
  674. syms_of_indent ()
  675. {
  676.   DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode,
  677.     "*Indentation can insert tabs if this is non-nil.\n\
  678. Setting this variable automatically makes it local to the current buffer.");
  679.   indent_tabs_mode = 1;
  680.  
  681.   defsubr (&Scurrent_indentation);
  682.   defsubr (&Sindent_to);
  683.   defsubr (&Scurrent_column);
  684.   defsubr (&Smove_to_column);
  685.   defsubr (&Svertical_motion);
  686. }
  687.